<?php

namespace V6;
class C_InvoiceList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '获取客户端发票列表';

    public function get(array $req, array $post, &$result)
    {
        try {
            /* 授权验证【开始】 */
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler->getById($req['userType'], $req['uid']);  // 判断当前用户是否存在
            if (!is_object($customer_cls) && $customer_cls->getEnterpriseId() == false) {
                return 'data error';
            }
            $enterprise_id = $customer_cls->getEnterpriseId();
            if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $enterprise_id, $req['t'], $req['k']) == false) {
                return 'data illegal';
            }
            /* 授权验证【结束】 */
            if (!array_key_exists('uid', $req) || empty($req['uid'])) {
                return 'require uid';//买家id
            }
            if (!array_key_exists('channelId', $req) || empty($req['channelId'])) {
                return 'require channelId';//渠道id
            }
            $l = isset($req['l']) ? $req['l'] : 20;
            $o = isset($req['o']) ? $req['o'] : 0;
            // 实例化列表类
            $tabulation = new \Cls\Tabulation();
            // 初始化列表类相关参数
            $tabulation->setWhere('enterprise_customer_id', $req['uid'])
                ->setWhere('enterprise_id', $enterprise_id)
                ->setOffset($o)
                ->setLimit($l)
                ->setOrderBy('id', 'desc');

            $invoice_handler = new \Biz\InvoiceHandler();
            $invoice_handler->getInvoiceList($tabulation);
            $invoice_list = array();
            $result = array();
            if ($tabulation->getSuccessStatus()) {
                /**
                 * @var \Cls\Invoice\Invoice $invoice_cls
                 */
                foreach ($tabulation->getData() as $invoice_cls) {
                    $invoice_tmp = array();
                    $invoice_tmp['id'] = $invoice_cls->getId(); //发票id
                    $invoice_tmp['status'] = \Cls\Invoice\Invoice::getStatusById($invoice_cls->getStatus()); //发票状态
                    $invoice_tmp['apply_code'] = $invoice_cls->getApplyCode(); // 申请编号
                    $invoice_tmp['receipt_type'] = \Cls\Invoice\Invoice::getReceiptTypeByType($invoice_cls->getReceiptType()); //开具类型
                    $invoice_tmp['price'] = $invoice_cls->getPrice(); // 发票金额
                    $invoice_tmp['title'] = $invoice_cls->getTitle(); // 发票抬头
                    $invoice_tmp['create_time'] = $invoice_cls->getCreateTime(); // 开票时间
                    $invoice_list[] = $invoice_tmp;
                }
                $total = $tabulation->getTotal();
                $result['list'] = $invoice_list;
                $result['total'] = $total;
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }
}

?>